#include <iostream>
#include <stdio.h>
#include <string>
#include <cstdlib>
#include <unistd.h>
#include <string.h>

// cgi处理

bool GetQueryString(std::string &query_string)
{
    bool result = false;
    std::string method = getenv("METHOD");
    std::cerr << "query_method:" << method << std::endl;
    if (method == "GET")
    {
        char *getret = getenv("QUERY_STRING");
        if (getret == NULL)
        {
            std::cerr << "GETENV ERROR!" << std::endl;
            return 1;
        }
        query_string = getret;
        result = true;
        std::cerr << "DEBUG GET: " << query_string << std::endl;
    }
    else if (method == "POST")
    {
        char *getret = getenv("CONTENT_LENGTH");
        if (getret == NULL)
        {
            std::cerr << "GETENV ERROR!" << std::endl;
            return 1;
        }
        int len = atoi(getenv("CONTENT_LENGTH"));
        char ch;
        while (len)
        {
            read(0, &ch, 1);
            query_string.push_back(ch);
            len--;
        }
        result = true;
        std::cerr << "DEBUG POST: " << query_string << std::endl;
    }
    else
    {
    }
    return result;
}

bool CutString(std::string &in, const std::string &sep, std::string &out1, std::string &out2)
{
    size_t pos = in.find(sep);
    if (pos != std::string::npos)
    {
        out1 = in.substr(0, pos);
        out2 = in.substr(pos + sep.size());
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
    std::string query_string;
    GetQueryString(query_string);
    std::string str1, str2;
    std::cerr << CutString(query_string, "&", str1, str2) << std::endl;
    std::string name1, name2;
    std::string value1, value2;
    // 可能想把数据进行某种计算(计算，搜索，登录等)，进行某种存储(注册)
    std::cerr << CutString(str1, "=", name1, value1) << std::endl;
    std::cerr << CutString(str2, "=", name2, value2) << std::endl;
    std::cerr << name1 << " : " << value1 << std::endl;
    std::cerr << name2 << " : " << value2 << std::endl;
    
    double x = atoi(value1.c_str());
    double y = atoi(value2.c_str());

    std::cout << "<!DOCTYPE html><html><head><title>My Beautiful Page</title><style>body {margin: 0;padding: 0;background-color: #f2f2f2;font-family: Arial, sans-serif;}.container {display: flex;justify-content: center;align-items: center;flex-direction: column;height: 100vh;}.row {background-color: #fff;padding: 20px;margin: 10px;border-radius: 5px;box-shadow: 0 0 10px rgba(0,0,0,0.2);text-align: center;font-size: 24px;font-weight: bold;color: #333;}</style></head><body><div class=\"container\">";
    // std::cout << "<div style=\"text-align:center;\">  <p style=\"margin:auto;\"> ";
    std::cout << "<div class=\"row\">" << x << " + " << y << " = " << x + y << "</div>";
    std::cout << "<div class=\"row\">" << x << " - " << y << " = " << x - y << "</div>";
    std::cout << "<div class=\"row\">" << x << " * " << y << " = " << x * y << "</div>";
    std::cout << "<div class=\"row\">" << x << " / " << y << " = " << x / y << "</div>";
    std::cout << "</div></body></html>";

    return 0;
}